using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Extensions.Configuration;

using Newtonsoft.Json;

namespace NP.BPMReportPlatform
{
    public class BpmReportPlatformHttpClient
    {
        /// <summary>
        /// HttpClient
        /// </summary>
        private readonly HttpClient _httpClient;
        
        public BpmReportPlatformHttpClient(HttpClient httpClient)
        {
            _httpClient = httpClient;
            _httpClient.Timeout = TimeSpan.FromMinutes(30);
        }
        
        /// <inheritdoc />
        public async Task<T> GetAsync<T>(string requestUri)
        {
            var content = await GetStringAsync(requestUri);

            return string.IsNullOrEmpty(content) ? default(T) : JsonConvert.DeserializeObject<T>(content);
        }

        /// <inheritdoc />
        public async Task<string> GetStringAsync(string requestUri)
        {
            return await GetStringAsync(requestUri, null);
        }

        public async Task<Stream> GetStreamAsync(string requestUri)
        {
            return await GetStreamAsync(requestUri, null);
        }

        /// <inheritdoc />
        public async Task<T> PostAsync<T>(string requestUri, object obj)
        {
            var json = obj != null ? JsonConvert.SerializeObject(obj) : string.Empty;

            return await PostAsync<T>(requestUri, json);
        }

        public async Task<T> PostAsync<T>(string requestUri, string json)
        {
            var content = await PostStringAsync(requestUri, json);

            return string.IsNullOrEmpty(content) ? default(T) : JsonConvert.DeserializeObject<T>(content);
        }

        /// <inheritdoc />
        public async Task<string> PostStringAsync(string requestUri, object obj)
        {
            return await PostStringAsync(requestUri, obj, null);
        }

        public async Task<string> PostStringAsync(string requestUri, string json)
        {
            return await PostStringAsync(requestUri, json, null);
        }

        public async Task<Stream> PostStreamAsync(string requestUri, object obj)
        {
            return await PostStreamAsync(requestUri, obj, null);
        }

        public async Task<Stream> PostStreamAsync(string requestUri, string json)
        {
            return await PostStreamAsync(requestUri, json, null);
        }

        /// <inheritdoc />
        public async Task<T> GetAsync<T>(string requestUri, Dictionary<string, string> headers)
        {
            var content = await GetStringAsync(requestUri, headers);

            return string.IsNullOrEmpty(content) ? default(T) : JsonConvert.DeserializeObject<T>(content);
        }

        /// <inheritdoc />
        public async Task<string> GetStringAsync(string requestUri, Dictionary<string, string> headers)
        {
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    if (_httpClient.DefaultRequestHeaders.Contains(item.Key))
                    {
                        _httpClient.DefaultRequestHeaders.Remove(item.Key);
                    }

                    _httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }

            var response = await _httpClient.GetAsync(requestUri);

            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }

            throw new NetworkInformationException((int)response.StatusCode);
        }
        
        /// <inheritdoc />
        public async Task<Stream> GetStreamAsync(string requestUri, Dictionary<string, string> headers)
        {
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    if (_httpClient.DefaultRequestHeaders.Contains(item.Key))
                    {
                        _httpClient.DefaultRequestHeaders.Remove(item.Key);
                    }

                    _httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }

            var response = await _httpClient.GetAsync(requestUri);

            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStreamAsync();
            }

            throw new NetworkInformationException((int)response.StatusCode);
        }

        /// <inheritdoc />
        public async Task<T> PostAsync<T>(string requestUri, object obj, Dictionary<string, string> headers)
        {
            var json = obj != null ? JsonConvert.SerializeObject(obj) : string.Empty;

            return await PostAsync<T>(requestUri, json, headers);
        }


        /// <inheritdoc />
        public async Task<string> PostStringAsync(string requestUri, object obj, Dictionary<string, string> headers)
        {
            var json = obj != null ? JsonConvert.SerializeObject(obj) : string.Empty;

            return await PostStringAsync(requestUri, json, headers);
        }

        public async Task<Stream> PostStreamAsync(string requestUri, object obj, Dictionary<string, string> headers)
        {
            var json = obj != null ? JsonConvert.SerializeObject(obj) : string.Empty;

            return await PostStreamAsync(requestUri, json, headers);
        }

        /// <inheritdoc />
        public async Task<T> PostAsync<T>(string requestUri, string json, Dictionary<string, string> headers)
        {
            var content = await PostStringAsync(requestUri, json, headers);

            return string.IsNullOrEmpty(content) ? default(T) : JsonConvert.DeserializeObject<T>(content);
        }

        /// <inheritdoc />
        public async Task<string> PostStringAsync(string requestUri, string json, Dictionary<string, string> headers)
        {
            var httpContent = new StringContent(json ?? string.Empty, Encoding.UTF8);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "utf-8" };
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    if (_httpClient.DefaultRequestHeaders.Contains(item.Key))
                    {
                        _httpClient.DefaultRequestHeaders.Remove(item.Key);
                    }
                    _httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }
            var response = await _httpClient.PostAsync(requestUri, httpContent);

            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStringAsync();
            }

            throw new NetworkInformationException((int)response.StatusCode);
        }
        
        /// <inheritdoc />
        public async Task<Stream> PostStreamAsync(string requestUri, string json, Dictionary<string, string> headers)
        {
            var httpContent = new StringContent(json ?? string.Empty, Encoding.UTF8);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json") { CharSet = "utf-8" };
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    if (_httpClient.DefaultRequestHeaders.Contains(item.Key))
                    {
                        _httpClient.DefaultRequestHeaders.Remove(item.Key);
                    }
                    _httpClient.DefaultRequestHeaders.Add(item.Key, item.Value);
                }
            }
            var response = await _httpClient.PostAsync(requestUri, httpContent);

            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsStreamAsync();
            }

            throw new NetworkInformationException((int)response.StatusCode);
        }
    }
}